package com.cloudeasy.area.service;

import com.cloudeasy.api.enums.ResultCodeEnum;
import com.cloudeasy.api.exception.CloudEasyException;
import com.cloudeasy.api.util.SnowFlake;
import com.cloudeasy.area.po.AreaAttention;
import com.cloudeasy.area.repository.AreaAttentionRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class AreaAttentionService {
    private final AreaService areaService;

    private final AreaAttentionRepository areaAttentionRepository;

    private final SnowFlake snowFlake;

    public AreaAttentionService(AreaService areaService, AreaAttentionRepository areaAttentionRepository, SnowFlake snowFlake) {
        this.areaService = areaService;
        this.areaAttentionRepository = areaAttentionRepository;
        this.snowFlake = snowFlake;
    }

    /**
     * 关注
     *
     * @param userId 用户Id
     * @param areaId 区域Id
     * @return {@link AreaAttention}
     */
    public AreaAttention attention(String userId, String areaId) {
        AreaAttention areaAttention = AreaAttention.builder().userId(userId).areaId(areaId).build();
        if (areaAttentionRepository.existsByUserIdAndAreaId(userId, areaId)) {
            throw new CloudEasyException(ResultCodeEnum.REPEAT_ERROR);
        }
        areaService.attention(areaId);
        areaAttention.setId(String.valueOf(snowFlake.nextId()));
        return areaAttentionRepository.save(areaAttention);
    }

    /**
     * 移除关注
     *
     * @param userId 用户Id
     * @param areaId 区域Id
     */
    public void removeAttention(String userId, String areaId) {
        if (!areaAttentionRepository.existsByUserIdAndAreaId(userId, areaId)) {
            throw new CloudEasyException(ResultCodeEnum.REPEAT_ERROR);
        }
        areaService.removeAttention(areaId);
        areaAttentionRepository.deleteByUserIdAndAreaId(userId, areaId);
    }

    public List<AreaAttention> list(String areaId) {
        return areaAttentionRepository.findAllByAreaIdIs(areaId);
    }

    /**
     * 删除区域相关的关注信息
     *
     * @param id Id
     */
    public void deleteAttention(String id) {
        areaAttentionRepository.deleteByAreaId(id);
    }

    /**
     * 根据区域Id分页查找记录
     *
     * @param areaId 区域Id
     * @param page   页数
     * @param size   每页大小
     * @return {@link Page<AreaAttention>}
     */
    public Page<AreaAttention> page(String areaId, Integer page, Integer size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return areaAttentionRepository.findAllByAreaIdIs(areaId, pageable);
    }

    /**
     * 获取用户关注的区域信息
     *
     * @param userId 用户Id
     * @return {@link List<AreaAttention>}
     */
    public List<AreaAttention> listByAttentionId(String userId) {
        return areaAttentionRepository.findAllByUserId(userId);
    }
}
